﻿Imports System.ComponentModel
Imports System.ComponentModel.Design

<DefaultEvent("GroupSelectEvent")> _
Public Class ProjectTypeControl

    Private _pi As ProjectItem


    Public Sub New()

        ' 此调用是 Windows 窗体设计器所必需的。
        InitializeComponent()

        ' 在 InitializeComponent() 调用之后添加任何初始化。
        Init()

    End Sub
        Private Sub Init()
        Me.tb_netid.Text = "0"
        Me.rb_DevTypeBig.Checked = True
        Me.rb_DevTypeSmall.Checked = False
        Me.Nud_RefNum.Value = 0
        Me.DevAppTypeItem.SelectedIndex = 1
        _groupSelected = False
    End Sub

    Private _dtiItem As DevTypeItem
    <Browsable(False)> _
    Public Property dtiItem() As DevTypeItem
        Get
            getItem()
            If _dtiItem Is Nothing Then
                _dtiItem = New DevTypeItem()
            End If

            Return _dtiItem

        End Get
        Set(ByVal value As DevTypeItem)
            _dtiItem = value
            SetItem(value)
        End Set
    End Property

    Private Sub getItem()

        If _dtiItem Is Nothing Then
            _dtiItem = New DevTypeItem()
        End If



        _dtiItem.NetId = Integer.Parse(Trim(Me.tb_netid.Text))
        _dtiItem.DevType = IIf(Me.rb_DevTypeBig.Checked, 1, 2)
        _dtiItem.RefNum = Integer.Parse(Trim(Me.Nud_RefNum.Value))

        '20100519李吉鹏增加扩展设备应用类型
        Select Case Me.DevAppTypeItem.SelectedIndex
            Case 0
                _dtiItem.DevAppType = 17
            Case 1
                _dtiItem.DevAppType = 18

        End Select

        
        _dtiItem.RefTypeGroupList = New DevTypeItem().RefTypeGroupList
        
        Dim rti As RefTypeItem
        For i As Integer = 0 To Me.FlowLayoutPanel2.Controls.Count - 1
            rti = CType(Me.FlowLayoutPanel2.Controls(i), ProjectTypeRef).refItem
            'rti.RefId = i + 1

            _dtiItem.RefTypeGroupList.Add(rti)


        Next
        

    End Sub
    
    Private Sub SetItem(ByVal value As DevTypeItem)


        If value.DevType = 1 Then
            Me.rb_DevTypeBig.Checked = True
        ElseIf value.DevType = 2 Then
            Me.rb_DevTypeSmall.Checked = True
        Else
            Me.rb_DevTypeBig.Checked = False
            Me.rb_DevTypeSmall.Checked = False
        End If

        Me.Nud_RefNum.Value = value.RefNum.ToString

        Select Case value.DevAppType
            Case 17
                Me.DevAppTypeItem.SelectedIndex = 0
            Case 18
                Me.DevAppTypeItem.SelectedIndex = 1

        End Select

        If value.NetId > 0 And value.NetId <= 256 Then
            Me.tb_netid.Text = value.NetId.ToString
        End If
        For Each item As RefTypeItem In value.RefTypeGroupList

            AddRefGroupControl(item)

        Next

    End Sub

    Private Sub CompressorGroupControl_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Click
        RaiseEvent GroupSelectEvent(Me, New EventArgs)
    End Sub

    Public Event GroupSelectEvent As EventHandler

    Private _groupSelected As Boolean
    Public Property groupSelected() As Boolean
        Get
            Return _groupSelected
        End Get
        Set(ByVal value As Boolean)
            _groupSelected = value
            Me.gbsel.Visible = value
        End Set
    End Property


    Public Sub SetEnable(ByVal b As Boolean)
        Me.Panel1.Enabled = b
        Me.Panel2.Enabled = b
        'Me.FlowLayoutPanel2.Enabled = b
    End Sub

    Public Sub SetNetId(ByVal val As Integer)
        Me.tb_netid.Text = val.ToString()
        If Integer.Parse(Trim(Me.tb_netid.Text)) = 1 Then ' 1号网络设备必须设置为大板类型
            Me.rb_DevTypeBig.Checked = True
            Me.rb_DevTypeSmall.Checked = False
            Me.rb_DevTypeSmall.Enabled = False
        End If

    End Sub

    Public Sub AddRefGroupControl(ByVal rti As RefTypeItem)
        Dim c As New ProjectTypeRef
        c.refItem = rti
        c.refSelected = False
        c.Panel1.Enabled = False
        AddHandler c.RefSelectEvent, AddressOf CompressorRefControl_RefSelectEvent
        Me.FlowLayoutPanel2.Controls.Add(c)


    End Sub
    Private Sub CompressorRefControl_RefSelectEvent(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim clrs As Color = Color.FromKnownColor(KnownColor.Control)

        For Each c As ProjectTypeRef In Me.FlowLayoutPanel2.Controls
            c.refSelected = False
            c.Panel1.Enabled = False
            c.gbref.BackColor = clrs
        Next

        CType(sender, ProjectTypeRef).refSelected = True
        CType(sender, ProjectTypeRef).gbref.BackColor = Color.FromKnownColor(KnownColor.Coral)
    End Sub
    Public Function GetSelectedControl() As ProjectTypeRef
        Dim item As ProjectTypeRef = Nothing

        For Each c As ProjectTypeRef In Me.FlowLayoutPanel2.Controls
            If c.refSelected Then
                item = c
                Exit For
            End If
        Next

        Return item
    End Function

    

    Private Sub DevAppTypeItem_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DevAppTypeItem.SelectedIndexChanged
        'Dim appvalue As Integer
        'appvalue = DevTypeService.GetdevAppType(MainForm.pi.ProjectID)

        Select Case sender.SelectedIndex
            Case 0
                Me.tb_netid.Text = GetAvailNetid(MainForm.pi.ProjectID, 17)
                'If appvalue <> 0 Then
                '    msg.ShowWarning("请将EX板配置在兼容板之前！")
                'End If


            Case 1
                Me.tb_netid.Text = GetAvailNetid(MainForm.pi.ProjectID, 18)

        End Select
    End Sub

    Private Function GetAvailNetid(ByVal pid As String, ByVal apptype As Integer) As Integer
        Return ProjectService.DevTypeMaxNetid(pid, apptype) + 1
    End Function
End Class